﻿<%@ Page Title="Permissions" Language="VB" MasterPageFile="~/MasterPage.master" Theme="Msn_Blue"  %>

<script runat="server">
    Dim description As String = ""
    Dim permissions1 As permissions = New permissions()
    
    
    
    
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
       
        If IsPostBack Then
            
            Response.Write("PostBack")
        
        End If
        
        Dim connection_string As String = ConfigurationManager.ConnectionStrings("conn").ConnectionString
        Dim SqlConnection1 As System.Data.SqlClient.SqlConnection = New System.Data.SqlClient.SqlConnection()
        SqlConnection1.ConnectionString = connection_string
        SqlConnection1.Open()

        Dim SqlCommand1 As System.Data.SqlClient.SqlCommand = New System.Data.SqlClient.SqlCommand()

        Dim string_sql As String = "Select * from Permissions"
        SqlCommand1.Connection = SqlConnection1 'associa o comando a executar com a conexão'
        SqlCommand1.CommandText = string_sql ' define o sql que o comando vai executar'
        GridView1.DataKeyNames = New String() {"id_permissions"}
        GridView1.DataSource = SqlCommand1.ExecuteReader()
        GridView1.DataBind()
        SqlConnection1.Close()   ' fecha a ligação à base de dados '

        
    End Sub
    
    Public Sub cleanfields()
    
        
        txt_id_permissions.Text = "0"
        txt_permissions.text = ""
        txt_description.text = ""
        
        
    End Sub
    
    Public Sub show_permissions_on_page()
        
        
        
        txt_id_permissions.Text = permissions1.id_permissions.ToString()
        txt_permissions.Text = permissions1.permissions
        txt_description.Text = permissions1.description
        
        
        
        
        
        
    End Sub
    

    Protected Sub show_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        show_permissions_on_page()
    End Sub
    
    
    Protected Sub clear_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        cleanfields()
        
    End Sub



    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        'insert
        permissions1.permissions = txt_permissions.Text
        permissions1.description = txt_description.Text
        permissions1.insert()
        
        lbl_info.Text = "Sucess."
        lbl_info.ForeColor = Drawing.Color.DarkGreen
        
       
    End Sub

    Protected Sub Button1_Click1(ByVal sender As Object, ByVal e As System.EventArgs)
        'update 
        permissions1.permissions = txt_permissions.Text
        permissions1.description = txt_description.Text
        permissions1.update()
                                                                         
        lbl_info.Text = "Sucess."
        lbl_info.ForeColor = Drawing.Color.DarkGreen
        
    End Sub

    Public Sub formfieltovariable()
        permissions1.id_permissions = Integer.Parse(txt_id_permissions.Text)
        permissions1.permissions = txt_permissions.Text
        permissions1.description = txt_description.Text
        
        
       
    End Sub
    
    
    
    Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        formfieltovariable()
        permissions1.first()
        variabletoformfield()
        
    End Sub
    
    
    Public Sub variabletoformfield()
        txt_id_permissions.Text = permissions1.id_permissions.ToString()
        txt_permissions.Text = permissions1.permissions
        txt_description.Text = permissions1.description
        
    End Sub
    
    
    Protected Sub Button3_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        formfieltovariable()
        permissions1.previous_permissions()
        variabletoformfield()
    End Sub

    Protected Sub Button4_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        formfieltovariable()
        permissions1.next_permissions()
        variabletoformfield()
    End Sub

    Protected Sub Button5_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        formfieltovariable()
        permissions1.last_permissions()
        variabletoformfield()
        
    End Sub

    Protected Sub Button6_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        formfieltovariable()
        
        Response.Write(permissions1.delet())
        cleanfields()
        
    End Sub

    Protected Sub GridView1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        
        Page.ClientScript.RegisterStartupScript("".GetType(), "ClienteScript", "alert('" + GridView1.SelectedValue.ToString() + " ');", True)
                    
        permissions1.id_permissions = Integer.Parse(GridView1.SelectedValue.ToString())
        
        'txt_id_permissions.Text = GridView1.SelectedValue.ToString()
        
        'txt_permissions.Text = GridView1.SelectedRow.Cells(2).Text
        'txt_description.Text = GridView1.SelectedRow.Cells(3).Text
 
        permissions1.id_permissions = GridView1.SelectedValue.ToString()
        permissions1.select_by_id()
        variabletoformfield()
        
        
                       
    End Sub
    
    Protected Sub GridView1_RowDeleting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewDeleteEventArgs)
        Page.ClientScript.RegisterStartupScript("".GetType(), "ClienteScript", "alert('rowindex:" + e.RowIndex.ToString() + "');", True)
    End Sub
</script>



<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">



    <style type="text/css">
        .style1
        {
        }
        .style2
        {
            height: 30px;
        }
        .style3
        {
            width: 546px;
        }
        .style4
        {
            height: 30px;
            width: 546px;
        }
        .style5
        {
            height: 16px;
            font-size: x-large;
        }
    </style>



</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">

    <table style="height: 57px; width: 65%">
        <tr>
            <td class="style5" colspan="2">
                Permissions</td>
        </tr>
        <tr>
            <td class="style1">
                <asp:Label ID="lbl_id_permissions" runat="server" Text="id_permissions"></asp:Label>
            </td>
            <td class="style3">
                <asp:TextBox ID="txt_id_permissions" runat="server" Width="571px">0</asp:TextBox>
            </td>
        </tr>
        <tr>
            <td class="style1">
                <asp:Label ID="lbl_permissions" runat="server" Text="permissions"></asp:Label>
            </td>
            <td class="style3">
                <asp:TextBox ID="txt_permissions" runat="server" Width="569px" 
                    style="margin-right: 0px"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td class="style1">
                <asp:Label ID="lbl_description" runat="server" Text="description"></asp:Label>
            </td>
            <td class="style3">
                <asp:TextBox ID="txt_description" runat="server" Width="568px"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td class="style2">
            </td>
            <td class="style4">
                <asp:Button ID="clear" runat="server" Text="Clear" style="margin-left: 8px" 
                    Width="51px" onclick="clear_Click" />
                <asp:Button ID="Insert" runat="server" onclick="Button1_Click" 
                    style="margin-left: 7px" Text="insert" />
            &nbsp;&nbsp;<asp:Button ID="Button1" runat="server" onclick="Button1_Click1" 
                    Text="Update" Width="50px" />
&nbsp;<asp:Button ID="Button2" runat="server" onclick="Button2_Click" Text="&lt;&lt;" 
                    Width="25px" />
                <asp:Button ID="Button3" runat="server" Text="&lt;" onclick="Button3_Click" />
                <asp:Button ID="Button4" runat="server" Text="&gt;" onclick="Button4_Click" />
                <asp:Button ID="Button5" runat="server" Text="&gt;&gt;" 
                    onclick="Button5_Click" />
                <asp:Button ID="Button6" runat="server" Text="Delete" onclick="Button6_Click" />
                <asp:Label ID="lbl_info" runat="server"></asp:Label>
            </td>
        </tr>
        <tr>
            <td class="style2" colspan="2">
                
                
            
                <asp:GridView ID="GridView1" runat="server" 
                    onselectedindexchanged="GridView1_SelectedIndexChanged" 
                    onrowdeleting="GridView1_RowDeleting">
                    <Columns>
                        <asp:CommandField HeaderText="Select" ShowHeader="True" 
                            ShowSelectButton="True" />
                        <asp:CommandField HeaderText="Delete" ShowDeleteButton="True" 
                            ShowHeader="True" />
                    </Columns>
                </asp:GridView>
                
                
            
            </td>
        </tr>
    </table>

  

</asp:Content>


